package p.lodz.pl.poid.zad2.controller;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import p.lodz.pl.poid.zad2.segmentation.QuadNode;
import p.lodz.pl.poid.zad2.segmentation.SegmentationProcessor;

/**
 * FXML Controller class
 *
 * @author janiu
 */
public class SegmentationPanelController implements Initializable {
    
    @FXML
    private RadioButton simple;
    
    @FXML
    private RadioButton euclidean;
    
    @FXML
    private RadioButton luminance;
    
    @FXML
    private Slider value;
    
    @FXML
    private ComboBox<String> regions;
    
    @FXML
    private CheckBox areaColor;
    
    @FXML
    private CheckBox inverse;
    
    @FXML
    private CheckBox showGrid;
    
    @FXML
    private ComboBox<String> fillType;
    
    private BufferedImage bufferedImage;
    private ImageView resultImage;
    private Image image;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        fillTypes();
    }    
    
    @FXML
    private void performSegmentation(ActionEvent event) {
        SegmentationData data = prepareData();
        SegmentationProcessor processor = new SegmentationProcessor(data);
        processor.performSegmentation(this.bufferedImage);
        BufferedImage bufferedImage = processor.getResultImage();
        Image image = Image.impl_fromExternalImage(bufferedImage);
        this.resultImage.setImage(image);
        fillRegions(processor.getRegions());
    }
    
    private SegmentationData prepareData() {
        SegmentationData data = new SegmentationData();
        boolean isSimpleComparer = simple.isSelected();
        boolean isEuclideanComparer = euclidean.isSelected();
        boolean isLuminanceComparer = luminance.isSelected();
        boolean isInverse = inverse.isSelected();
        boolean isAreaColor = areaColor.isSelected();
        boolean isShowGrid = showGrid.isSelected();
        Integer fillTypeNumber = fillType.getSelectionModel().getSelectedIndex();        
        Integer value = (int) this.value.getValue();
        Integer region = regions.getSelectionModel().getSelectedIndex();
        data.setSimpleComparison(isSimpleComparer);
        data.setEuclideanComparison(isEuclideanComparer);
        data.setLuminanceComparison(isLuminanceComparer);
        data.setInverse(isInverse);
        data.setColorArea(isAreaColor);
        data.setShowGrid(isShowGrid);
        data.setFillOption(fillTypeNumber);
        data.setValue(value);
        data.setRegionNumber(region);
        return data;
    }
    
    private void fillRegions(HashMap<Integer, QuadNode> regions) {
        List<String> regionsStringList = new ArrayList<>();
        for(int i = 0; i < regions.size(); i++) {
            regionsStringList.add("Region " + i);
        }
        ObservableList<String> observableList = FXCollections.observableArrayList(regionsStringList);
        this.regions.setItems(observableList);
    }
    
    private void fillTypes() {
        List<String> fillTypesStringList = new ArrayList<>();
        fillTypesStringList.add("No fill");
        fillTypesStringList.add("Fill all");
        fillTypesStringList.add("Fill only selected");
        fillTypesStringList.add("Mask fill");
        fillTypesStringList.add("Transparent mask fill");
        ObservableList<String> observableList = FXCollections.observableArrayList(fillTypesStringList);
        this.fillType.setItems(observableList);
        this.fillType.getSelectionModel().select(0);
    }
    
    public void setResultImage(ImageView resultImage) {
        this.resultImage = resultImage;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

}
